package com.dmyang.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 切面编程
 * Created by brss on 2018/3/23.
 */
public class DataSourceAspect {

    public void before(JoinPoint joinPoint) throws NoSuchMethodException {
        Object target = joinPoint.getTarget();
        String method = joinPoint.getSignature().getName();

        Class clazz = target.getClass();
        Class<?>[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();
        Method m = clazz.getMethod(method,parameterTypes);

        if (m!=null && m.isAnnotationPresent(DataSource.class)){
            DataSource dataSource = m.getAnnotation(DataSource.class);
            DynamicDataSourceHolder.setDataSource(dataSource.value());
        }
    }
}
